import time

from PyQt5.QtCore import QThread, QObject, pyqtSignal
from PyQt5.QtWidgets import QWidget, QPushButton, QLineEdit, QPlainTextEdit
from PyQt5.uic import loadUi

from base_api.HelloWorldClass import HelloWorld
from base_api.MessageClass import Message
from pyqt_api.HelloWorldSignalThreadClass import HelloWorldSignalThread
from pyqt_api.HelloWorldTaskClass import HelloWorldTask
from pyqt_api.SignalThreadClass import SignalThread

"""
主窗口
"""


class HomeWindow:
    def __init__(self):
        # 加载ui文件创建ui对象
        self.window = loadUi('ui/home.ui')
        self.window: QWidget
        # 显示该窗口对象
        self.window.show()
        # 按钮，事件监听 绑定方法
        self.window.pushButton: QPushButton
        self.window.pushButton.clicked.connect(self.pushButton_clicked)

        # 工作标志
        self.work_flag = False

    def pushButton_clicked(self):
        if not self.work_flag:
            self.work_flag = True
            # 读取输入
            self.window.lineEdit: QLineEdit
            s = self.window.lineEdit.text()

            """信号类pyqtSignal演示"""
            helloWorldTask = HelloWorldTask()
            helloWorldTask.name = s
            helloWorldTask.signal.connect(self.output)
            helloWorldTask.execute()

            """pyqtSignal+QThread演示"""
            # helloWorldSignalThread = HelloWorldSignalThread()
            # helloWorldSignalThread.name = s
            # helloWorldSignalThread.signal.connect(self.output)
            # helloWorldSignalThread.start()

            """泛任务类演示"""
            # signalThread = SignalThread()
            # signalThread.signal.connect(self.output)
            # # 普通的任务类
            # helloWorld = HelloWorld()
            # helloWorld.name = s
            # signalThread.execute_method = helloWorld.say
            # helloWorld.output_access = signalThread.output
            # signalThread.start()

    def output(self, msg: Message):
        # 线程执行完毕
        if msg.status == -1:
            self.work_flag = False
        # 线程执行过程中输出
        elif msg.status == 1:
            self.window.plainTextEdit: QPlainTextEdit
            self.window.plainTextEdit.appendPlainText(str(msg.data))

